package com.pur.zsms.matermanage.mater.service.impl;

import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import net.sf.cglib.beans.BeanCopier;

import org.springframework.stereotype.Service;

import com.pur.zsms.common.exception.GenericException;
import com.pur.zsms.common.support.PageController;
import com.pur.zsms.common.support.PaginationSupport;
import com.pur.zsms.common.util.Cn2Spell;
import com.pur.zsms.matermanage.mater.dao.IMaterDao;
import com.pur.zsms.matermanage.mater.po.CheckMaterCostPo;
import com.pur.zsms.matermanage.mater.po.CheckMaterSalePo;
import com.pur.zsms.matermanage.mater.po.MaterLevelBlPo;
import com.pur.zsms.matermanage.mater.po.MaterLevelCblPo;
import com.pur.zsms.matermanage.mater.po.MaterLevelPo;
import com.pur.zsms.matermanage.mater.po.MaterPo;
import com.pur.zsms.matermanage.mater.po.MaterTypePo;
import com.pur.zsms.matermanage.mater.po.ShoppingCartMaterPo;
import com.pur.zsms.matermanage.mater.po.ShoppingCartMaterSelfPo;
import com.pur.zsms.matermanage.mater.po.ShoppingCartMaterSupplierPo;
import com.pur.zsms.matermanage.mater.po.UnitMaterConvertPo;
import com.pur.zsms.matermanage.mater.service.IMaterService;
import com.pur.zsms.matermanage.mater.vo.MaterLevelVo;
import com.pur.zsms.matermanage.mater.vo.MaterTypeVo;
import com.pur.zsms.matermanage.mater.vo.MaterValueVo;
import com.pur.zsms.matermanage.mater.vo.MaterVo;
import com.pur.zsms.matermanage.mater.vo.MaterVoForSup;
import com.pur.zsms.matermanage.mater.vo.MaterVoForUnit;
import com.pur.zsms.matermanage.mater.vo.ShoppingCartMaterSupplierVo;
import com.pur.zsms.matermanage.mater.vo.ShoppingCartMaterVo;
import com.pur.zsms.pmsnmanage.user.po.LdAddressPo;
import com.pur.zsms.po.DistributorSupplierMaterPo;
import com.pur.zsms.supplier.dao.SupplierDao;
import com.pur.zsms.supplier.po.SupplierPo;
import com.pur.zsms.systemsetting.datadictionary.dao.IDataDictionaryDao;
import com.pur.zsms.systemsetting.datadictionary.po.Code;
import com.pur.zsms.vo.DistributorSupplierMaterVo;

@Service("materService")
public class MaterServiceImpl implements IMaterService {

	String msg = "";

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	@Resource(name = "dataDictionaryDao")
	private IDataDictionaryDao dataDictionaryDao;

	public IDataDictionaryDao getDataDictionaryDao() {
		return dataDictionaryDao;
	}

	public void setDataDictionaryDao(IDataDictionaryDao dataDictionaryDao) {
		this.dataDictionaryDao = dataDictionaryDao;
	}

	@Resource(name = "materDao")
	private IMaterDao materDaoImpl;

	public IMaterDao getMaterDaoImpl() {
		return materDaoImpl;
	}

	public void setMaterDaoImpl(IMaterDao foodDaoImpl) {
		this.materDaoImpl = foodDaoImpl;
	}

	@Resource(name = "supplierDao")
	private SupplierDao supplierDao;

	public SupplierDao getSupplierDao() {
		return supplierDao;
	}

	public void setSupplierDao(SupplierDao supplierDao) {
		this.supplierDao = supplierDao;
	}

	public PaginationSupport listMater(PageController pageController, MaterVo materVo) throws GenericException {
		return materDaoImpl.listMater(pageController, materVo);
	}

	public PaginationSupport listMaterUnit(PageController pageController, MaterVoForUnit materVo) throws GenericException {
		return materDaoImpl.listMaterUnit(pageController, materVo);
	}

	public PaginationSupport lookMaterUnit(PageController pageController, MaterVoForUnit materVo) throws GenericException {
		return materDaoImpl.lookMaterUnit(pageController, materVo);
	}

	public PaginationSupport listMaterForSup(PageController pageController, MaterVoForSup materVo) throws GenericException {
		return materDaoImpl.listMaterForSup(pageController, materVo);
	}

	public PaginationSupport lookMaterValue(PageController pageController, MaterVo materVo) throws GenericException {
		return materDaoImpl.lookMaterValue(pageController, materVo);
	}

	public PaginationSupport lookMaterValueCost(PageController pageController, MaterVo materVo) throws GenericException {
		return materDaoImpl.lookMaterValueCost(pageController, materVo);
	}

	public PaginationSupport listEditorMater(PageController pageController, MaterVo materVo) throws GenericException {
		return materDaoImpl.listEditorMater(pageController, materVo);
	}

	public PaginationSupport listSelectMater(PageController pageController, MaterVo materVo) throws GenericException {
		return materDaoImpl.listSelectMater(pageController, materVo);
	}

	public PaginationSupport findMaterTypeForSup(PageController pageController, MaterTypeVo vo) throws GenericException {
		return materDaoImpl.findMaterTypeForSup(pageController, vo);
	}

	public List<Object[]> listSelectMaterForMobile(MaterVo materVo) throws GenericException {
		return materDaoImpl.listSelectMaterForMobile(materVo);

	}

	public List<Object[]> initShoppingMaterForMobile(MaterVo materVo) throws GenericException {
		return materDaoImpl.initShoppingMaterForMobile(materVo);

	}

	public List<Object[]> listSelectMaterTypeForMobile(MaterTypeVo c) throws GenericException {
		return materDaoImpl.listSelectMaterTypeForMobile(c);
	}

	public PaginationSupport listSelectMaterForBuySupplier(PageController pageController, MaterVo materVo) throws GenericException {
		return materDaoImpl.listSelectMaterForBuySupplier(pageController, materVo);

	}

	public PaginationSupport listSelectMaterSelf(PageController pageController, MaterVo materVo) throws GenericException {
		return materDaoImpl.listSelectMaterSelf(pageController, materVo);
	}

	public PaginationSupport findShoppingCartMater(PageController pageController, ShoppingCartMaterVo shoppingCartMaterVo) throws GenericException {
		return materDaoImpl.findShoppingCartMater(pageController, shoppingCartMaterVo);
	}

	public PaginationSupport findShoppingCartMaterSupplier(PageController pageController, ShoppingCartMaterVo shoppingCartMaterVo)
			throws GenericException {
		return materDaoImpl.findShoppingCartMaterSupplier(pageController, shoppingCartMaterVo);

	}

	public PaginationSupport findShoppingCartMaterSelf(PageController pageController, ShoppingCartMaterVo shoppingCartMaterVo)
			throws GenericException {
		return materDaoImpl.findShoppingCartMaterSelf(pageController, shoppingCartMaterVo);
	}

	public boolean addMater(MaterVo c) throws GenericException {

		MaterPo po = new MaterPo();
		MaterLevelPo tMaterLevelPo = new MaterLevelPo();

		// 校验相同名 相同品牌 相同规格 相同supid 是否存在
		List<MaterLevelPo> materLevelPoOldList = null;
		materLevelPoOldList = materDaoImpl.getMaterLevelPoByMaterNameUnitSpecificationsBrandsSupid(c.getMaterName(), c.getUnit(),
				c.getSpecifications(), c.getBrands(), c.getSupId());

		if (materLevelPoOldList != null && materLevelPoOldList.size() > 0) {
			msg = "msg0002";
			return false;
		}

		// 取得同名的材料插入相同的materid主键
		List<MaterPo> materPoOldList = null;
		materPoOldList = materDaoImpl.getMaterPoByMaterNameUnitSpecificationsBrands(c.getMaterName(), c.getUnit(), c.getSpecifications(),
				c.getBrands());
		if (materPoOldList != null && materPoOldList.size() > 0) {// 已有材料id的分支

			// 材料种类的校验
			if (materPoOldList.get(0).getMaterType() != c.getMaterType()) {
				msg = "msg0001";
				return false;
			}

			po.setMaterId(materPoOldList.get(0).getMaterId());
			po.setMaterName(c.getMaterName());
			po.setMaterType(c.getMaterType());

			po.setUnit(c.getUnit());
			po.setSpecifications(c.getSpecifications());
			po.setBrands(c.getBrands());

			po.setAllSpell(Cn2Spell.converterToSpell(po.getMaterName()));
			po.setFistSpell(Cn2Spell.converterToFirstSpell(po.getMaterName()));

			materDaoImpl.addMater(po);

			tMaterLevelPo.setMaterId(materPoOldList.get(0).getMaterId());
			tMaterLevelPo.setMaterLevel(c.getMaterLevel());
			tMaterLevelPo.setMaterProportion(c.getMaterProportion());

			BigDecimal materProportion = new BigDecimal(c.getMaterProportion());
			MathContext mc = new MathContext(5, RoundingMode.HALF_UP);
			BigDecimal materValueNet = c.getMaterValueUndressed().multiply(new BigDecimal(0.1)).divide(materProportion, mc)
					.setScale(5, BigDecimal.ROUND_HALF_UP);

			tMaterLevelPo.setMaterValueUndressed(c.getMaterValueUndressed().multiply(new BigDecimal(0.001)));
			tMaterLevelPo.setMaterValue(materValueNet);

			tMaterLevelPo.setMaterDescribe(c.getMaterDescribe());
			tMaterLevelPo.setSupId(c.getSupId());
			materDaoImpl.updateMaterLevel(tMaterLevelPo);
		} else {
			// 产生新材料的分支
			po.setMaterName(c.getMaterName());
			po.setMaterType(c.getMaterType());
			po.setUnit(c.getUnit());
			po.setSpecifications(c.getSpecifications());
			po.setBrands(c.getBrands());

			po.setAllSpell(Cn2Spell.converterToSpell(po.getMaterName()));
			po.setFistSpell(Cn2Spell.converterToFirstSpell(po.getMaterName()));
			int id = materDaoImpl.addMater(po);

			tMaterLevelPo.setMaterId(id);
			tMaterLevelPo.setMaterLevel(c.getMaterLevel());
			tMaterLevelPo.setMaterProportion(c.getMaterProportion());
			tMaterLevelPo.setMaterDescribe(c.getMaterDescribe());

			BigDecimal materProportion = new BigDecimal(c.getMaterProportion());
			MathContext mc = new MathContext(5, RoundingMode.HALF_UP);
			BigDecimal materValueNet = c.getMaterValueUndressed().multiply(new BigDecimal(0.1)).divide(materProportion, mc)
					.setScale(5, BigDecimal.ROUND_HALF_UP);

			tMaterLevelPo.setMaterValueUndressed(c.getMaterValueUndressed().multiply(new BigDecimal(0.001)));
			tMaterLevelPo.setMaterValue(materValueNet);

			tMaterLevelPo.setSupId(c.getSupId());

			materDaoImpl.updateMaterLevel(tMaterLevelPo);
		}
		return true;
	}

	public boolean addMaterTypePo(MaterTypeVo c) throws GenericException {
		MaterTypePo po = new MaterTypePo();
		po.setIsActive("0");
		if (c.getParent() == 0) {
			po.setLevel(0);
		} else {
			po.setLevel(1);
		}
		po.setMaterType(c.getMaterType());
		po.setMaterTypeName(c.getMaterTypeName());
		po.setOrderId(c.getOrderId());
		po.setParent(c.getParent());
		po.setSupId(c.getSupId());
		materDaoImpl.addMaterTypePo(po);
		return true;
	}

	public boolean updateMaterTypePo(MaterTypeVo c) throws GenericException {
		MaterTypePo po = materDaoImpl.getMaterTypePo(c).get(0);
		po.setIsActive("0");
		if (c.getParent() == 0) {
			po.setLevel(0);
		} else {
			po.setLevel(1);
		}
		po.setMaterType(c.getMaterType());
		po.setMaterTypeName(c.getMaterTypeName());
		po.setOrderId(c.getOrderId());
		po.setParent(c.getParent());
		po.setSupId(c.getSupId());
		materDaoImpl.updateMaterTypePo(po);
		return true;
	}

	public void addMaterLevel(MaterLevelPo c) throws GenericException {
		materDaoImpl.addMaterLevel(c);
	}

	public boolean addCheckMaterCost(MaterVoForSup c) throws GenericException {
		if (c.getCheckCostId() == 0) {
			CheckMaterCostPo po = new CheckMaterCostPo();
			po.setLowLimit(c.getLowLimit());
			po.setUpperLimit(c.getUpperLimit());
			po.setSupId(c.getSupId());
			po.setMaterId(c.getMaterId());
			materDaoImpl.addcheckMaterCostPo(po);
		} else {
			CheckMaterCostPo po = materDaoImpl.getCheckMaterCostPoById(c.getCheckCostId()).get(0);
			po.setLowLimit(c.getLowLimit());
			po.setUpperLimit(c.getUpperLimit());
			po.setMaterId(c.getMaterId());
			materDaoImpl.updatecheckMaterCostPo(po);
		}

		if (c.getCheckSaleId() == 0) {
			CheckMaterSalePo po = new CheckMaterSalePo();
			po.setLowLimitSale(c.getLowLimitSale());
			po.setUpperLimitSale(c.getUpperLimitSale());
			po.setSupId(c.getSupId());
			po.setMaterId(c.getMaterId());
			materDaoImpl.addcheckMaterSalePo(po);
		} else {
			CheckMaterSalePo po = materDaoImpl.getCheckMaterSalePoById(c.getCheckSaleId()).get(0);
			po.setLowLimitSale(c.getLowLimitSale());
			po.setUpperLimitSale(c.getUpperLimitSale());
			po.setMaterId(c.getMaterId());
			materDaoImpl.updatecheckMaterSalePo(po);
		}

		return true;
	}

	public MaterVo getMaterVo(String c) throws GenericException {
		MaterPo materPo = materDaoImpl.listMaterPoByMaterId(c);
		MaterVo materVo = new MaterVo();

		BeanCopier copier = BeanCopier.create(MaterPo.class, MaterVo.class, false);
		copier.copy(materPo, materVo, null);

		MaterLevelPo materLevelPo = materDaoImpl.listMaterLevelPoByMaterId(c);

		BeanCopier copier2 = BeanCopier.create(MaterLevelPo.class, MaterVo.class, false);
		copier2.copy(materLevelPo, materVo, null);

		return materVo;
	}

	public MaterPo getMaterPo(int materId) throws GenericException {
		return materDaoImpl.getMaterPoByMaterId(materId).get(0);
	}

	public MaterPo getMaterPo(String barcode) throws GenericException {

		List<MaterPo> materPolist = materDaoImpl.getMaterPoByBarcode(barcode);
		if (materPolist != null && materPolist.size() > 0) {
			return materPolist.get(0);
		} else {
			return null;
		}

	}

	public MaterVo getMaterLevelVo(String materId, String supId, String materLevel) throws GenericException {
		MaterVo materVo = new MaterVo();

		MaterPo materPo = materDaoImpl.listMaterPoByMaterId(materId);

		BeanCopier copier = BeanCopier.create(MaterPo.class, MaterVo.class, false);
		copier.copy(materPo, materVo, null);

		MaterLevelPo materLevelPo = materDaoImpl.listMaterLevelPoByMaterIdSupIdLevelIdCustId(materId, supId, materLevel, 0);

		BeanCopier copier2 = BeanCopier.create(MaterLevelPo.class, MaterVo.class, false);
		copier2.copy(materLevelPo, materVo, null);

		materVo.setMaterValueUndressed(materVo.getMaterValueUndressed().multiply(new BigDecimal("1000")));
		return materVo;
	}

	public MaterTypePo getMaterTypePo(MaterTypeVo vo) throws GenericException {
		return materDaoImpl.getMaterTypePo(vo).get(0);
	}

	public BigDecimal getMaterLevelValue(int materId) throws GenericException {
		return materDaoImpl.getMaterLevelValue(materId).get(0).getMaterValueUndressed().multiply(new BigDecimal(1000));
	}

	public MaterValueVo getMaterValueVo(MaterValueVo materValueVo) throws GenericException {
		List<MaterLevelBlPo> materLevelPoList = materDaoImpl.listMaterLevelBlPoByMaterIdSupIdLevelId(materValueVo);

		if (materLevelPoList != null && materLevelPoList.size() > 0) {
			StringBuffer arryDateAndValue = new StringBuffer("");
			arryDateAndValue.append("[");
			for (MaterLevelBlPo materLevelBlPo : materLevelPoList) {
				arryDateAndValue.append("['");
				arryDateAndValue.append(materLevelBlPo.getMaterDay());
				arryDateAndValue.append("',");
				arryDateAndValue.append(materLevelBlPo.getMaterValueUndressed().multiply(new BigDecimal("1000")));
				arryDateAndValue.append("]");
				arryDateAndValue.append(",");
			}
			arryDateAndValue.delete(arryDateAndValue.lastIndexOf(","), arryDateAndValue.lastIndexOf(",") + 1);
			arryDateAndValue.append("]");
			materValueVo.setMaterValueAndDateArray(arryDateAndValue.toString());

			MaterPo materPo = materDaoImpl.getMaterPoByMaterId(materValueVo.getMaterId()).get(0);
			materValueVo.setMaterName(materPo.getMaterName());
			materValueVo.setUnit(materPo.getUnit());
			SupplierPo supplierPo = supplierDao.getSupPoBySupId(String.valueOf(materValueVo.getSupId()));
			materValueVo.setSupName(supplierPo.getCompanyName());
		}

		return materValueVo;
	}

	public MaterValueVo getMaterValueVoCost(MaterValueVo materValueVo) throws GenericException {
		List<MaterLevelCblPo> materLevelPoList = materDaoImpl.listMaterLevelBlPoForMaterCost(materValueVo);

		if (materLevelPoList != null && materLevelPoList.size() > 0) {
			StringBuffer arryDateAndValue = new StringBuffer("");
			arryDateAndValue.append("[");
			for (MaterLevelCblPo materLevelBlPo : materLevelPoList) {
				arryDateAndValue.append("['");
				arryDateAndValue.append(materLevelBlPo.getMaterDay());
				arryDateAndValue.append("',");
				arryDateAndValue.append(materLevelBlPo.getMaterValueUndressed().multiply(new BigDecimal("1000")));
				arryDateAndValue.append("]");
				arryDateAndValue.append(",");
			}
			arryDateAndValue.delete(arryDateAndValue.lastIndexOf(","), arryDateAndValue.lastIndexOf(",") + 1);
			arryDateAndValue.append("]");
			materValueVo.setMaterValueAndDateArray(arryDateAndValue.toString());

			MaterPo materPo = materDaoImpl.getMaterPoByMaterId(materValueVo.getMaterId()).get(0);
			materValueVo.setMaterName(materPo.getMaterName());
			materValueVo.setUnit(materPo.getUnit());
			SupplierPo supplierPo = supplierDao.getSupPoBySupId(String.valueOf(materValueVo.getSupId()));
			materValueVo.setSupName(supplierPo.getCompanyName());
		}

		return materValueVo;
	}

	public BigDecimal getMaterValueCostLast(int materId, long supId) throws GenericException {
		BigDecimal materValueCost = materDaoImpl.getMaterValueCostLast(materId, supId);
		return materValueCost;
	}

	/**
	 * 材料更新
	 * */
	public String updateMater(MaterVo c) throws GenericException {

		// 校验相同名 相同品牌 相同规格 相同supid 是否存在
		List<Integer> listCheck = null;
		listCheck = materDaoImpl.getMaterLevelPoByMaterNameUnitSpecificationsBrandsSupidForUpdate(c.getMaterName(), c.getUnit(),
				c.getSpecifications(), c.getBrands(), c.getSupId(), c.getMaterType());

		if (listCheck != null && listCheck.size() > 0) {
			for (Integer integer : listCheck) {
				if (integer.intValue() != c.getMaterId()) {
					msg = "msg0002";
					return msg;
				}
			}
		}

		MaterPo po = new MaterPo();
		BeanCopier copier1 = BeanCopier.create(MaterVo.class, MaterPo.class, false);
		copier1.copy(c, po, null);
		po.setAllSpell(Cn2Spell.converterToSpell(po.getMaterName()));
		po.setFistSpell(Cn2Spell.converterToFirstSpell(po.getMaterName()));
		materDaoImpl.updateMater(po);

		MaterLevelPo tMaterLevelPo = new MaterLevelPo();
		BeanCopier copier2 = BeanCopier.create(MaterVo.class, MaterLevelPo.class, false);
		copier2.copy(c, tMaterLevelPo, null);

		BigDecimal materProportion = new BigDecimal(c.getMaterProportion());

		MathContext mc = new MathContext(5, RoundingMode.HALF_UP);
		BigDecimal materValueNet = c.getMaterValueUndressed().multiply(new BigDecimal(0.1)).divide(materProportion, mc)
				.setScale(5, BigDecimal.ROUND_HALF_UP);
		tMaterLevelPo.setMaterValueUndressed(c.getMaterValueUndressed().multiply(new BigDecimal(0.001)));
		tMaterLevelPo.setMaterValue(materValueNet);

		materDaoImpl.updateMaterLevel(tMaterLevelPo);
		return "0000";
	}

	public void deleteMater(int materId, int materLevel, int supId) throws GenericException {
		MaterLevelPo materlevelPo = materDaoImpl.getMaterLevelPoByMaterIdAndLevelAndSupId(materId, materLevel, supId, "0").get(0);
		materlevelPo.setDeleteFlag(1);

		materDaoImpl.deleteMaterLevel(materlevelPo);
	}

	public void deleteMaterUnitHostMaterId(int materId, int supId) throws GenericException {
		materDaoImpl.deleteMaterUnitHostMaterId(materId, supId);
	}

	public void deleteMaterUnitAuxiliaryMaterId(int materId, int supId) throws GenericException {
		materDaoImpl.deleteMaterUnitAuxiliaryMaterId(materId, supId);
	}

	public void recoverMater(int materId, int materLevel, int supId) throws GenericException {
		MaterLevelPo materlevelPo = materDaoImpl.getMaterLevelPoByMaterIdAndLevelAndSupId(materId, materLevel, supId, "0").get(0);
		materlevelPo.setDeleteFlag(0);
		materDaoImpl.deleteMaterLevel(materlevelPo);
	}

	public MaterLevelPo copyMaterForSup(int materId, int level, int supId, int supIdCopy) throws GenericException {
		MaterLevelVo materLevelVo = new MaterLevelVo();

		MaterLevelPo materLevelPo = new MaterLevelPo();
		// 检验 是否 供货商 存在这种商品
		List<MaterLevelPo> materLevelPoList1 = materDaoImpl.getMaterLevelPoByMaterIdAndLevelAndSupId(materId, level, supId, "0");

		if (materLevelPoList1.size() > 0) {
			msg = "选中的供货商已有此种材料！";
			return null;
		}
		List<MaterLevelPo> materLevelPoList2 = materDaoImpl.getMaterLevelPoByMaterIdAndLevelAndSupId(materId, level, supIdCopy, "0");
		MaterLevelPo materLevelPoCopy = materLevelPoList2.get(0);
		materLevelPoCopy.setMid(0);
		materLevelPoCopy.setSupId(supId);

		materLevelPo.setCustId(0);
		materLevelPo.setDeleteFlag(materLevelPoCopy.getDeleteFlag());
		materLevelPo.setMaterDescribe(materLevelPoCopy.getMaterDescribe());
		materLevelPo.setMaterId(materLevelPoCopy.getMaterId());
		materLevelPo.setMaterLevel(materLevelPoCopy.getMaterLevel());
		materLevelPo.setMaterProportion(materLevelPoCopy.getMaterProportion());
		materLevelPo.setMaterValue(materLevelPoCopy.getMaterValue());
		materLevelPo.setMaterValueUndressed(materLevelPoCopy.getMaterValueUndressed());
		materLevelPo.setSupId(supId);

		/*
		 * BeanCopier copier = BeanCopier.create(MaterLevelPo.class,
		 * MaterLevelVo.class, false); copier.copy(materLevelPoCopy,
		 * materLevelVo, null);
		 * 
		 * BeanCopier copier2 = BeanCopier.create(MaterLevelVo.class,
		 * MaterLevelPo.class, false); copier2.copy(materLevelVo, materLevelPo,
		 * null);
		 */
		// materLevelPo.setMid(99999);
		// materDaoImpl.updateMaterLevel(materLevelPo);
		return materLevelPo;
	}

	public void backupMater() throws GenericException {
		List<MaterLevelPo> materLevelPoList = materDaoImpl.getAllMaterLevelList();

		BeanCopier copier = BeanCopier.create(MaterLevelPo.class, MaterLevelBlPo.class, false);
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");// 设置日期格式
		String day = df.format(new Date());
		for (MaterLevelPo materLevelPo : materLevelPoList) {
			MaterLevelBlPo materLevelBlPo = new MaterLevelBlPo();
			copier.copy(materLevelPo, materLevelBlPo, null);
			materLevelBlPo.setMaterDay(day);
			materDaoImpl.addMaterLevelBl(materLevelBlPo);
		}

	}

	public List<Object[]> getAllExcelMater() throws GenericException {
		return materDaoImpl.getAllExcelMater();
	}

	public List<Object[]> getAllExcelMaterCust(long userId) throws GenericException {
		return materDaoImpl.getAllExcelMaterCust(userId);
	}

	public void converterSpell() throws GenericException {
		List<MaterPo> materPoList = materDaoImpl.getAllMaterList();
		if (materPoList != null && materPoList.size() > 0) {
			for (MaterPo materPo : materPoList) {
				materPo.setAllSpell(Cn2Spell.converterToSpell(materPo.getMaterName()));
				materPo.setFistSpell(Cn2Spell.converterToFirstSpell(materPo.getMaterName()));
				materDaoImpl.updateMater(materPo);
			}
		}
	}

	public String getAllMaterTypeJsonString(int space) throws GenericException {
		List<Code> codeList = dataDictionaryDao.findCodeIdList("mater_type");
		StringBuffer ql = new StringBuffer();
		ql.append("[");
		if (space == 1) {
			ql.append("{\"materType\":\"0\",\"materTypename\":\"全部\"},");
		}
		String q2 = "";
		if (codeList != null && codeList.size() > 0) {
			for (Code code : codeList) {
				ql.append("{\"materType\":\"" + code.getCodeOder() + "\",\"materTypename\":\"" + code.getDescrib() + "\"},");
			}
			q2 = ql.substring(0, ql.length() - 1) + "]";
		}
		return q2;
	}

	public boolean addShoppingCart(ShoppingCartMaterVo c) throws GenericException {
		// check
		if (!addShoppingCartCheck(c)) {
			return false;
		}
		// check over

		ShoppingCartMaterPo shoppingCartMaterPo = new ShoppingCartMaterPo();
		BeanCopier copier = BeanCopier.create(ShoppingCartMaterVo.class, ShoppingCartMaterPo.class, false);
		copier.copy(c, shoppingCartMaterPo, null);
		shoppingCartMaterPo.setMaterValue(shoppingCartMaterPo.getMaterValue().multiply(new BigDecimal(0.001)));
		shoppingCartMaterPo.setMaterValueUndressed(shoppingCartMaterPo.getMaterValueUndressed().multiply(new BigDecimal(0.001)));
		materDaoImpl.addShoppingCart(shoppingCartMaterPo);

		return true;
	}

	public boolean addShoppingCartForInput(ShoppingCartMaterVo c) throws GenericException {
		ShoppingCartMaterPo shoppingCartMaterPo = new ShoppingCartMaterPo();

		List<ShoppingCartMaterPo> list = materDaoImpl.getShoppingCartMaterPo(c.getMaterId(), c.getCustId(), c.getSupId());
		if (list != null && list.size() > 0) {
			shoppingCartMaterPo = list.get(0);
			shoppingCartMaterPo.setMaterNum(c.getMaterNum());
			materDaoImpl.updateShoppingCart(shoppingCartMaterPo);

		} else {
			BeanCopier copier = BeanCopier.create(ShoppingCartMaterVo.class, ShoppingCartMaterPo.class, false);
			copier.copy(c, shoppingCartMaterPo, null);
			shoppingCartMaterPo.setMaterValue(shoppingCartMaterPo.getMaterValue().multiply(new BigDecimal(0.001)));
			shoppingCartMaterPo.setMaterValueUndressed(shoppingCartMaterPo.getMaterValueUndressed().multiply(new BigDecimal(0.001)));
			materDaoImpl.addShoppingCart(shoppingCartMaterPo);
		}

		return true;
	}

	public boolean addShoppingCartForAdd(ShoppingCartMaterVo c) throws GenericException {
		ShoppingCartMaterPo shoppingCartMaterPo = new ShoppingCartMaterPo();
		c.setMaterNum(new BigDecimal(1));

		List<ShoppingCartMaterPo> list = materDaoImpl.getShoppingCartMaterPo(c.getMaterId(), c.getCustId(), c.getSupId());
		if (list != null && list.size() > 0) {
			shoppingCartMaterPo = list.get(0);
			shoppingCartMaterPo.setMaterNum(shoppingCartMaterPo.getMaterNum().add(new BigDecimal(1)));
			materDaoImpl.updateShoppingCart(shoppingCartMaterPo);

		} else {
			BeanCopier copier = BeanCopier.create(ShoppingCartMaterVo.class, ShoppingCartMaterPo.class, false);
			copier.copy(c, shoppingCartMaterPo, null);
			shoppingCartMaterPo.setMaterValue(shoppingCartMaterPo.getMaterValue().multiply(new BigDecimal(0.001)));
			shoppingCartMaterPo.setMaterValueUndressed(shoppingCartMaterPo.getMaterValueUndressed().multiply(new BigDecimal(0.001)));
			materDaoImpl.addShoppingCart(shoppingCartMaterPo);
		}

		return true;
	}

	public boolean addShoppingCartForMin(ShoppingCartMaterVo c) throws GenericException {
		ShoppingCartMaterPo shoppingCartMaterPo = new ShoppingCartMaterPo();
		c.setMaterNum(new BigDecimal(1));

		List<ShoppingCartMaterPo> list = materDaoImpl.getShoppingCartMaterPo(c.getMaterId(), c.getCustId(), c.getSupId());
		if (list != null && list.size() > 0) {
			shoppingCartMaterPo = list.get(0);

			// 判断负数
			BigDecimal finishNum = shoppingCartMaterPo.getMaterNum().subtract(new BigDecimal(1));

			if (new BigDecimal(0).compareTo(finishNum) < 0) {
				shoppingCartMaterPo.setMaterNum(shoppingCartMaterPo.getMaterNum().subtract(new BigDecimal(1)));
				materDaoImpl.updateShoppingCart(shoppingCartMaterPo);
			} else {
				c.setSid(shoppingCartMaterPo.getSid());
				materDaoImpl.deleteShoppingCartOne(c);
			}
		}

		return true;
	}

	public int getShopCartNumForShop(ShoppingCartMaterVo c) throws GenericException {
		List<ShoppingCartMaterPo> list = materDaoImpl.getShoppingCartMaterPo(c.getCustId());
		if (list != null && list.size() > 0) {
			return list.size();
		} else {
			return 0;
		}
	}

	public boolean addShoppingCartForMobileInput(ShoppingCartMaterVo c) throws GenericException {
		ShoppingCartMaterPo shoppingCartMaterPo = new ShoppingCartMaterPo();

		List<ShoppingCartMaterPo> list = materDaoImpl.getShoppingCartMaterPo(c.getMaterId(), c.getCustId(), c.getSupId());

		BeanCopier copier = BeanCopier.create(ShoppingCartMaterVo.class, ShoppingCartMaterPo.class, false);
		copier.copy(c, shoppingCartMaterPo, null);
		// shoppingCartMaterPo.setMaterValue(shoppingCartMaterPo.getMaterValue().multiply(new
		// BigDecimal(0.001)));
		shoppingCartMaterPo.setMaterValueUndressed(shoppingCartMaterPo.getMaterValueUndressed().multiply(new BigDecimal(0.001)));
		materDaoImpl.addShoppingCart(shoppingCartMaterPo);

		return true;
	}

	public boolean addShoppingCartSup(ShoppingCartMaterSupplierVo c) throws GenericException {
		// check
		if (!addShoppingCartCheckSup(c)) {
			return false;
		}
		// check over

		ShoppingCartMaterSupplierPo shoppingCartMaterSupPo = new ShoppingCartMaterSupplierPo();
		shoppingCartMaterSupPo.setSupId(c.getSupId());
		shoppingCartMaterSupPo.setMaterId(c.getMaterId());
		shoppingCartMaterSupPo.setMaterLevel(c.getMaterLevel());
		shoppingCartMaterSupPo.setMaterNum(c.getMaterNum());
		shoppingCartMaterSupPo.setLeaveMessage(c.getLeaveMessage());
		materDaoImpl.addShoppingCartSup(shoppingCartMaterSupPo);
		return true;
	}

	public boolean addShoppingCartSelf(ShoppingCartMaterVo c) throws GenericException {
		// check
		if (!addShoppingCartCheckSelf(c)) {
			return false;
		}
		// check over

		ShoppingCartMaterSelfPo shoppingCartMaterSelfPo = new ShoppingCartMaterSelfPo();
		BeanCopier copier = BeanCopier.create(ShoppingCartMaterVo.class, ShoppingCartMaterSelfPo.class, false);
		copier.copy(c, shoppingCartMaterSelfPo, null);
		shoppingCartMaterSelfPo.setMaterValueUndressed(shoppingCartMaterSelfPo.getMaterValueUndressed().multiply(new BigDecimal(0.001)));
		materDaoImpl.addShoppingCartSelf(shoppingCartMaterSelfPo);
		return true;
	}

	public boolean deleteShoppingCartOne(ShoppingCartMaterVo c) throws GenericException {
		materDaoImpl.deleteShoppingCartOne(c);
		return true;
	}

	public boolean deleteShoppingCartOneSelf(ShoppingCartMaterVo c) throws GenericException {
		materDaoImpl.deleteShoppingCartOneSelf(c);
		return true;
	}

	public boolean deleteShoppingCartSup(ShoppingCartMaterVo c) throws GenericException {
		materDaoImpl.deleteShoppingCartSup(c);
		return true;
	}

	public boolean deleteShoppingCartSupAll(int c) throws GenericException {
		materDaoImpl.deleteShoppingCartSupAll(c);
		return true;
	}

	public boolean deleteShoppingCartForCust(long shopId) throws GenericException {
		materDaoImpl.deleteShoppingCartForCust(shopId);
		return true;
	}

	public boolean deleteShoppingCartForCustSelf(long custId) throws GenericException {
		materDaoImpl.deleteShoppingCartForCustSelf(custId);
		return true;
	}

	public boolean addShoppingCartCheck(ShoppingCartMaterVo c) throws GenericException {
		// check
		List<ShoppingCartMaterPo> list = materDaoImpl.getShoppingCartMaterPo(c.getMaterId(), c.getCustId(), c.getSupId());
		if (list != null && list.size() > 0) {
			msg = "msg003";
			return false;
		}
		// check over
		return true;
	}

	public boolean addShoppingCartCheckSelf(ShoppingCartMaterVo c) throws GenericException {
		// check
		List<ShoppingCartMaterSelfPo> list = materDaoImpl.getShoppingCartMaterSelfPo(c.getMaterId(), c.getCustId());
		if (list != null && list.size() > 0) {
			msg = "msg003";
			return false;
		}
		// check over
		return true;
	}

	public boolean addShoppingCartCheckSup(ShoppingCartMaterSupplierVo c) throws GenericException {
		// check
		List<ShoppingCartMaterSupplierPo> list = materDaoImpl.getShoppingCartMaterSupPo(c.getMaterId(), c.getSupId());
		if (list != null && list.size() > 0) {
			msg = "msg003";
			return false;
		}
		// check over
		return true;
	}

	public boolean addMaterUnit(MaterVoForUnit c) throws GenericException {
		String materIdArray[] = c.getMaterIdAll().split(",");
		// 校验子不能等于父辈
		for (String materIdAuxiliary : materIdArray) {
			int materIdSun = Integer.valueOf(materIdAuxiliary);
			if (materIdSun == c.getMaterId()) {
				msg = "附属材料不能和主材料相同！";
				return false;
			}
			// 校验子材料不能是主材料
			List<UnitMaterConvertPo> tUnitMaterConvertPoList = materDaoImpl.getUnitMaterConvertPobyHostMaterId(c.getSupId(), materIdSun);
			if (tUnitMaterConvertPoList != null && tUnitMaterConvertPoList.size() > 0) {
				msg = "附属材料(" + materIdSun + ")已经是 主材料！";
				return false;
			}
		}

		// 删除主单位 和附属单位关联
		materDaoImpl.deleteMaterUnit(c.getMaterId(), c.getSupId());

		for (String materIdAuxiliary : materIdArray) {
			materDaoImpl.deleteMaterUnit(Integer.valueOf(materIdAuxiliary), c.getSupId());
			UnitMaterConvertPo po = new UnitMaterConvertPo();
			po.setAuxiliaryMaterId(Integer.valueOf(materIdAuxiliary));
			po.setSupId(c.getSupId());
			po.setHostMaterId(c.getMaterId());
			materDaoImpl.addUnitMaterConvertPo(po);
		}
		return true;
	}

	// 绑定供货商和材料给分销商
	public boolean bindingMaterSupOne(MaterVoForSup c) throws GenericException {
		// 先删除原有供货商
		DistributorSupplierMaterVo vo = new DistributorSupplierMaterVo();
		vo.setDistributorId(c.getDistributorId());
		vo.setMaterId(c.getMaterId());
		List<DistributorSupplierMaterPo> list = supplierDao.getDistributorSupplierMaterPo(vo);
		for (DistributorSupplierMaterPo distributorSupplierMaterPo : list) {
			supplierDao.deleteDistributorSupplierMaterPo(distributorSupplierMaterPo);
		}
		// 添加现有供货商
		DistributorSupplierMaterPo po = new DistributorSupplierMaterPo();
		po.setDistributorId(c.getDistributorId());
		po.setMaterId(c.getMaterId());
		po.setSupId(c.getSupId());
		supplierDao.addDistributorSupplierMaterPo(po);

		return true;
	}

	public boolean bindingMaterSupBatch(MaterVoForSup c) throws GenericException {
		// 先删除原有供货商
		DistributorSupplierMaterVo vo = new DistributorSupplierMaterVo();
		vo.setDistributorId(c.getDistributorId());
		vo.setMaterType(c.getMaterType());
		List<DistributorSupplierMaterPo> list = supplierDao.getDistributorSupplierMaterPo(vo);
		for (DistributorSupplierMaterPo distributorSupplierMaterPo : list) {
			// 删除
			supplierDao.deleteDistributorSupplierMaterPo(distributorSupplierMaterPo);
		}
		// 添加现有供货商
		MaterVo materVo = new MaterVo();
		materVo.setSupId((int) c.getDistributorId());// 设定 分销商检索条件
		materVo.setMaterType(c.getMaterType());
		List<MaterPo> listMater = materDaoImpl.getMaterPo(materVo);
		for (MaterPo materPo : listMater) {
			DistributorSupplierMaterPo po = new DistributorSupplierMaterPo();
			po.setDistributorId(c.getDistributorId());
			po.setMaterId(materPo.getMaterId());
			po.setSupId(c.getSupId());
			supplierDao.addDistributorSupplierMaterPo(po);
		}
		return true;

	}

	public List<ShoppingCartMaterPo> getShoppingCartMaterPo(long shopId) throws GenericException {
		return materDaoImpl.getShoppingCartMaterPo(shopId);
	}

	public String pulldownMaterTypeParentForSup(String space, long supId) throws GenericException {
		MaterTypeVo materTypeVo = new MaterTypeVo();
		materTypeVo.setSupId(supId);
		List<MaterTypePo> materTypeList = materDaoImpl.getMaterTypePo(materTypeVo);
		StringBuffer jsonMaterType = new StringBuffer("");

		if (materTypeList != null && materTypeList.size() > 0) {

			jsonMaterType.append("[");
			if ("1".equals(space)) {
				jsonMaterType.append("{\"");
				jsonMaterType.append("materType");
				jsonMaterType.append("\":\"");
				jsonMaterType.append("0");
				jsonMaterType.append("\",\"materTypeName\":\"");
				jsonMaterType.append("无");
				jsonMaterType.append("\"},");
			}

			for (MaterTypePo tMaterTypePo : materTypeList) {
				jsonMaterType.append("{\"");
				jsonMaterType.append("materType");
				jsonMaterType.append("\":\"");
				jsonMaterType.append(tMaterTypePo.getMaterType());
				jsonMaterType.append("\",\"materTypeName\":\"");
				jsonMaterType.append(tMaterTypePo.getMaterTypeName());
				jsonMaterType.append("\"},");
			}
			jsonMaterType = new StringBuffer(jsonMaterType.substring(0, jsonMaterType.length() - 1));
			jsonMaterType.append("]");
		}
		return jsonMaterType.toString();
	}

}
